<?php

/**
 * Create an form input field
 * 
 * @package Framework
 * @subpackage Controls
 * 
 * @access public
 */
class email_autocomplete extends html_element
{
	var $hidden_input;
	var $text_input;
	var $container;
	var $xml_source;
	var $xml_fields;
	var $xml_root_element;
	var $form_name;
	var $submit_on_enter;
	var $log=false;
	var $lookup_url='';

	function get_header()
	{
		global $GO_CONFIG;
		
		$header = '';
		
		if(!defined('YAHOO_LOADED'))
		{
			$header= '<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/yahoo/yahoo-min.js"></script>'.
			'<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/dom/dom-min.js"></script>'.
			'<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/event/event-min.js"></script>'.
			'<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/connection/connection-min.js"></script>'.
			'<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/animation/animation-min.js"></script>';
			if($this->log)
			{
				$header .= '<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/logger/logger.js"></script>';
			}
			
			define('YAHOO_LOADED',true);
		}
		
		if(!defined('YAHOO_AC_LOADED'))
		{	
			$header .= '<script type="text/javascript" src="'.$GO_CONFIG->host.'yui/build/autocomplete/autocomplete-min.js"></script>';
			define('YAHOO_AC_LOADED',true);
		}


		$header .= '
		<script type="text/javascript">
		YAHOO.namespace ("'.$this->attributes['id'].'");
		YAHOO.'.$this->attributes['id'].'.ACXml = function(){
			var oACDS;
			var oAutoComp;
			var mylogger;

			return {

				init: function() {
				';
				if($this->log)
				{
					$header .= 'mylogger = new YAHOO.widget.LogReader("'.$this->attributes['id'].'_logger");';
				}
		$header .= '
					oACDS = new YAHOO.widget.DS_XHR("'.$this->xml_source.'",["'.$this->xml_root_element.'"';
					$fields=array();
					foreach($this->xml_fields as $xml_field=>$form_field)
					{
						$fields[]=$xml_field;
					}
					$header .= ',"'.implode('","', $fields).'"';					
					
					$header .=']);
					oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
					oACDS.queryMatchContains = true;
					oACDS.scriptQueryParam = "query";
					

					oAutoComp = new YAHOO.widget.AutoComplete("'.$this->text_input->attributes['id'].'","'.$this->container->attributes['id'].'", oACDS);
					oAutoComp.queryDelay=0.5;
					oAutoComp.autoHighlight = true;
					oAutoComp.delimChar=",";
					oAutoComp.useIFrame=true;
					
					oAutoComp.formatResult = function(oResultItem, sQuery) {					
						return \'"\'+oResultItem[1]+\'" &lt;\'+oResultItem[2]+\'&gt;\';
					};
				}
			};
		}();
		YAHOO.util.Event.addListener(this,"load",YAHOO.'.$this->attributes['id'].'.ACXml.init);
		</script>';
					
		return $header;
	}
	

	function email_autocomplete($id='autocomplete', $text_name, $text_value, 
		$form_name)
	{
		global $GO_MODULES;
		
		$this->form_name=$form_name;
		$this->xml_source=$GO_MODULES->modules['addressbook']['url'].'search_email_xml.php';
		$this->xml_fields=array('full_email'=>'','name'=>'','email'=>'');
		$this->xml_root_element='contact';
		
		$this->set_attribute('id', $id);
		$this->set_attribute('class','autocomplete');
		$this->tagname = 'div';
		$this->set_linebreak("\n");
		
	
		$this->text_input = new textarea($text_name,$text_value);		
		$this->text_input->set_attribute('onfocus', 'this.select();');
		//$this->text_input->set_attribute('onchange', 'YAHOO.'.$this->attributes['id'].'.ACXml.changed();');
		$this->text_input->set_attribute('id',$id.'_text_input');
		$this->text_input->set_attribute('class','autocomplete_input');

		$this->container = new html_element('div');
		$this->container->set_attribute('id', $id.'_container');
		$this->container->set_attribute('class','autocomplete_container');
		
		
	}

	function get_html()
	{
		if(isset($this->hidden_input))
		{
			$this->add_html_element($this->hidden_input);		
		}
		$this->add_html_element($this->text_input);				
		if(!empty($this->lookup_url))
		{
			$img = new image('magnifier');
			$img->set_attribute('align','absmiddle');
			$img->set_attribute('style','border:0px;');
			
			$link = new hyperlink('javascript:YAHOO.'.$this->attributes['id'].'.ACXml.lookup();', $img->get_html());
			$this->add_html_element($link);
				
		}
		$this->add_html_element($this->container);
		
		if($this->log)
		{
			$div = new html_element('div');
			$div->set_attribute('id',$this->attributes['id'].'_logger');
			$this->add_html_element($div);
		}
		
		return $this->get_header().parent::get_html();
	}
}
